package debby.bdmap.utils

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.widget.Button
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import debby.baidumap.R
import com.baidu.mapapi.model.LatLngBounds
import com.baidu.mapapi.map.BaiduMap.OnMarkerDragListener
import debby.bdmap.interfaceutil.MainUtils
import debby.bdmap.view.getJobView


/**
 * Created by debby on 2018/6/11.
 * 描述:图层绘制
 */

//mark点
fun drawMarks(bdMap: BaiduMap, point: LatLng) {
    val bitmap = BitmapDescriptorFactory
            .fromResource(R.mipmap.ic_start)
    val option = MarkerOptions()
            .position(point)
            .icon(bitmap)
            .zIndex(9)
            .scaleX(1.2f)
            .scaleY(1.2f)
            .draggable(true)
    val overlay = bdMap.addOverlay(option)
    addOverlay(One, overlay)
    //调用BaiduMap对象的setOnMarkerDragListener方法设置Marker拖拽的监听
    bdMap.setOnMarkerDragListener(object : OnMarkerDragListener {
        override fun onMarkerDrag(marker: Marker) {
            //拖拽中
            Log.e("marker", "拖拽中")
        }

        override fun onMarkerDragEnd(marker: Marker) {
            //拖拽结束
            Log.e("marker", "拖拽结束")
        }

        override fun onMarkerDragStart(marker: Marker) {
            //开始拖拽
            Log.e("marker", "开始拖拽")
        }
    })
}

//mark点
fun drawOneMark(bdMap: BaiduMap, point: LatLng) {
    val bitmap = BitmapDescriptorFactory
            .fromResource(R.mipmap.ic_end)
    val option = MarkerOptions()
            .position(point)
            .icon(bitmap)
            .zIndex(9)
            .scaleX(1.2f)
            .scaleY(1.2f)
            .draggable(true)
    removeOverlay(OneMark)
    val oldMark = bdMap.addOverlay(option)
    addOverlay(OneMark, oldMark)
}

//线
fun drawLine(bdMap: BaiduMap, list: ArrayList<LatLng>) {
    val colors = ArrayList<Int>()
    colors.add(Color.parseColor("#46a8ea"))
    colors.add(Color.parseColor("#fd3358"))
    colors.add(Color.parseColor("#89d173"))
    colors.add(Color.parseColor("#ece470"))
    val polyline = PolylineOptions().width(10)
            .colorsValues(colors)
            .points(list)
    removeOverlay(OneLine)
    val oldLine = bdMap.addOverlay(polyline)
    addOverlay(OneLine, oldLine)
    drawOneMark(bdMap, list[list.size - 1])
}

//绘制多边形
fun drawArea(bdMap: BaiduMap, list: ArrayList<LatLng>) {
    val polygon = PolygonOptions()
            .points(list)
            .stroke(Stroke(5, Color.parseColor("#FFFFFF")))
            .fillColor(Color.parseColor("#46a8ea"))
    //在地图上添加多边形Option，用于显示
    removeOverlay(OnePolygon)
    val oldPolygon = bdMap.addOverlay(polygon);
    addOverlay(OnePolygon, oldPolygon)
}

//绘制文字
fun drawText(bdMap: BaiduMap) {
    val center = LatLng(31.9867, 118.6895)
    val textOverly = TextOptions()
            .bgColor(Color.parseColor("#46a8ea"))
            .fontColor(Color.parseColor("#FFFFFF"))
            .text("公司")
            .fontSize(100)
            .position(center)
            .rotate(24f)
    val oneText = bdMap.addOverlay(textOverly)
    addOverlay(OneText, oneText)
}

//绘制信息窗
fun drawInfoWindow(bdMap: BaiduMap, context: Context) {
    val center = LatLng(31.98700000, 118.69140000)
    val infoWindow = InfoWindow(getJobView(context), center, 0)
    bdMap.showInfoWindow(infoWindow)
}

//撤销
fun revoke(bdMap: BaiduMap, mainUtils: MainUtils) {
    if (pointList.size == 0) return
    when {
        pointList.size > 2 -> {
            pointList.removeAt(pointList.size - 1)
            drawLine(bdMap, pointList)
        }
        pointList.size > 1 -> {
            pointList.removeAt(pointList.size - 1)
            removeOverlay(OneLine)
            removeOverlay(OneMark)
        }
        else -> {
            clearOverlay(mainUtils)
        }
    }
    measureDistance(mainUtils)
}

//移动到指定地点以及显示范围
fun moveTo(bdMap: BaiduMap, point: LatLng) {
    val bounds = LatLngBounds.Builder().include(point).build()
    bdMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(bounds, 200, 200))
}

//清除所有绘制图层
fun clearOverlay(mainUtils: MainUtils) {
    removeAll()
    pointList.clear()
    mainUtils.setDistance("")
}

